package com.example.hrsystem.data.source;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;

import android.content.Context;

import com.example.hrsystem.data.model.AttendanceRecord;
import com.example.hrsystem.data.model.TaskItem;
import com.example.hrsystem.data.model.User;
import com.example.hrsystem.model.DailyAttendanceRecord;
import com.example.hrsystem.dao.DailyAttendanceDao;
import com.example.hrsystem.workflow.model.ProcessDefinition;
import com.example.hrsystem.workflow.model.ProcessInstance;
import com.example.hrsystem.workflow.model.WorkflowTask;
import com.example.hrsystem.workflow.model.ApprovalRequest;
import com.example.hrsystem.workflow.model.BusinessTripRequest;
import com.example.hrsystem.workflow.model.LeaveRequest;
import com.example.hrsystem.workflow.model.OvertimeRequest;

/**
 * 应用数据库
 * 使用Room持久化库管理SQLite数据库
 */
@Database(entities = {AttendanceRecord.class, TaskItem.class, User.class, DailyAttendanceRecord.class, 
    ProcessDefinition.class, ProcessInstance.class, WorkflowTask.class, 
    LeaveRequest.class, OvertimeRequest.class, BusinessTripRequest.class}, version = 7, exportSchema = false)
@TypeConverters({DateTypeConverters.class})
public abstract class AppDatabase extends RoomDatabase {

    private static volatile AppDatabase INSTANCE;

    public abstract AttendanceDao attendanceDao();
    public abstract TaskDao taskDao();
    public abstract UserDao userDao();
    public abstract com.example.hrsystem.dao.DailyAttendanceDao dailyAttendanceDao();
    
    // 工作流相关DAO
    public abstract ProcessDefinitionDao processDefinitionDao();
    public abstract ProcessInstanceDao processInstanceDao();
    public abstract WorkflowTaskDao workflowTaskDao();
    
    // 审批相关DAO
    public abstract LeaveRequestDao leaveRequestDao();
    public abstract OvertimeRequestDao overtimeRequestDao();
    public abstract BusinessTripRequestDao businessTripRequestDao();

    public static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                    AppDatabase.class, "hr_system_database")
                            .fallbackToDestructiveMigration()
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}